<template>
  <el-container>
    <el-main>
      <el-form label-width="90px" :model="form.model" :rules="form.rules" ref="formRef">
        <template v-if="modal.params.materialType == 'link' && form.model.content">
          <el-form-item label="标题" prop="content.title">
            <el-input v-model="form.model.content.title" placeholder="请输入标题"></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="content.description">
            <el-input
              v-model="form.model.content.description"
              placeholder="请输入描述"
              type="textarea"
            ></el-input>
          </el-form-item>
          <el-form-item label="图片" prop="content.image">
            <sa-uploader v-model="form.model.content.image" fileType="image"></sa-uploader>
          </el-form-item>
          <el-form-item label="链接地址" prop="content.url">
            <el-input v-model="form.model.content.url" placeholder="请输入链接地址"></el-input>
          </el-form-item>
        </template>
        <template v-if="modal.params.materialType == 'text'">
          <el-form-item label="内容" prop="content">
            <el-input
              v-model="form.model.content"
              placeholder="请输入内容"
              type="textarea"
            ></el-input>
          </el-form-item>
          <el-form-item label="超链接">
            <el-popover
              v-model:visible="linkPopover.visible"
              placement="bottom"
              trigger="click"
              :width="300"
            >
              <el-form
                ref="linkFormRef"
                :model="linkPopover.form.model"
                :rules="linkPopover.form.rules"
                label-width="80px"
              >
                <el-form-item label="文本内容" prop="text">
                  <el-input v-model="linkPopover.form.model.text" placeholder="请输入文本内容" />
                </el-form-item>
                <el-form-item label="链接地址" prop="href">
                  <el-input v-model="linkPopover.form.model.href" placeholder="请输入链接地址" />
                </el-form-item>
              </el-form>
              <div class="sa-flex sa-row-right">
                <el-button class="is-link" type="info" @click="cancelLinkPopover">取消</el-button>
                <el-button class="is-link" type="primary" @click="confirmLinkPopover"
                  >确定</el-button
                >
              </div>
              <template #reference v-if="modal.params.materialType == 'text'">
                <el-button class="is-link" type="primary">插入超链接</el-button>
              </template>
            </el-popover>
          </el-form-item>
        </template>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button
        v-if="modal.params.type == 'add'"
        v-auth="'wechat.admin.material.add'"
        type="primary"
        @click="confirm"
        >保存</el-button
      >
      <el-button
        v-if="modal.params.type == 'edit'"
        v-auth="'wechat.admin.material.edit'"
        type="primary"
        @click="confirm"
        >更新</el-button
      >
    </el-footer>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref, getCurrentInstance, unref } from 'vue';
  import wechat from '@/app/wechat/api';
  import { ElNotification } from 'element-plus';
  import { cloneDeep } from 'lodash';
  const emit = defineEmits(['modalCallBack']);
  const props = defineProps({
    modal: {
      type: Object,
    },
  });
  const { proxy } = getCurrentInstance();

  const defaultForm = {
    text: {
      type: props.modal.params.materialType,
      content: '',
    },
    link: {
      type: props.modal.params.materialType,
      content: { title: '', url: '', image: '', description: '' },
    },
  };
  const form = reactive({
    model: {},
    rules: {
      content: [
        {
          required: true,
          message: '请输入内容',
          trigger: 'blur',
        },
      ],
      'content.title': [
        {
          required: true,
          message: '请输入标题',
          trigger: 'blur',
        },
      ],
      'content.url': [
        {
          required: true,
          message: '请输入链接地址',
          trigger: 'blur',
        },
      ],
      'content.image': [
        {
          required: true,
          message: '请选择图片',
          trigger: 'blur',
        },
      ],
      'content.description': [
        {
          required: true,
          message: '请输入描述',
          trigger: 'blur',
        },
      ],
    },
  });
  const loading = ref(false);
  // 获取详情
  async function getDetail(id) {
    loading.value = true;
    const { error, data } = await wechat.admin.material.detail(id);
    error === 0 && (form.model = data);
    loading.value = false;
  }

  // 插入超链接
  const linkFormRef = ref();
  const linkPopover = reactive({
    visible: false,
    form: {
      model: {
        text: '',
        href: '',
      },
      rules: {
        text: [{ required: true, message: '请输入文本内容', trigger: 'blur' }],
        href: [{ required: true, message: '请输入链接地址', trigger: 'blur' }],
      },
    },
  });
  function cancelLinkPopover() {
    linkPopover.visible = false;
    linkPopover.form.model = {
      text: '',
      href: '',
    };
  }
  function confirmLinkPopover() {
    unref(linkFormRef) &&
      unref(linkFormRef).validate(async (valid) => {
        if (!valid) return;
        form.model.content += `<a href="${linkPopover.form.model.href}" target="_blank">${linkPopover.form.model.text}</a>`;
        cancelLinkPopover();
      });
  }

  // 表单关闭时提交
  function confirm() {
    proxy.$refs['formRef'].validate(async (valid) => {
      if (!valid) return;
      let submitForm = cloneDeep(form.model);
      if (props.modal.params.materialType == 'text') {
        submitForm.content = encodeURIComponent(submitForm.content);
      }
      const { error } =
        props.modal.params.type == 'add'
          ? await wechat.admin.material.add(submitForm)
          : await wechat.admin.material.edit(props.modal.params.id, submitForm);
      if (error == 0) {
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }

  onMounted(() => {
    if (props.modal.params.type == 'add') {
      form.model = defaultForm[props.modal.params.materialType];
    } else if (props.modal.params.type == 'edit') {
      getDetail(props.modal.params.id);
    }
  });
</script>
<style lang="scss" scoped></style>
